package lion.framework.core.web.anno;

import java.lang.annotation.Annotation;

import lion.dev.lang.BeanUtil;
import lion.dev.lang.MapJ;
import lion.framework.core.anno.AnnoProcessor;
import lion.framework.core.anno.AnnotationProcessorManager;
import lion.framework.core.anno.IAnnotationProcessor;
import lion.framework.core.web.exception.IExceptionHandler;
import lion.framework.core.web.exception.WEBExceptionHandlers;
import lion.framework.core.web.path.PathVar;

@AnnoProcessor(ExceptionHandler.class)
public class ExceptionHandlerProcessor implements IAnnotationProcessor {

	@Override
	public void processe(MapJ ctx, Annotation anno) throws Exception {

		Class<?> clazz = ctx.getE(AnnotationProcessorManager.ANNO_CONTEXT_TARGET);
		if (!IExceptionHandler.class.isAssignableFrom(clazz)) { return; }

		ExceptionHandler exceptionHandler = (ExceptionHandler) anno;
		IExceptionHandler handler = (IExceptionHandler) clazz.newInstance();
		String path = PathVar.replacePathVars(exceptionHandler.value());

		BeanUtil bu = new BeanUtil(handler);
		bu.setValue("path", path);

		WEBExceptionHandlers.addHandler(path, handler);

	}
}
